package com.chuji.crmcj.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.ClientTradeAdapter;
import com.chuji.crmcj.adapter.ListViewAdapter;
import com.chuji.crmcj.utils.CommonUtil;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BuyCarInforActivity extends BaseActivity implements View.OnClickListener{
    //返回
    private ImageView mIv_Back;
    //
    private ScrollView mScrollView;
    //购车用途
    private LinearLayout mLl_buy_use;
    //购车通途显示
    private TextView mTv_use;
    //购买预算
    private LinearLayout mLl_buy_budget;
    //购买预算显示
    private TextView mTv_budget;
    //购买方式
    private LinearLayout mLl_buy_way;
    //购买方式显示
    private TextView mTv_buy_way;
    //车辆使用人
    private LinearLayout mLl_car_user;
    //车辆使用人显示
    private TextView mTv_car_user;
    //意向车系
    private LinearLayout mLl_intent_car_series;
    //意向车系显示
    private TextView mTv_intent_car_series;
    //意向车型
    private LinearLayout mLl_intent_car_type;
    //意向车型显示
    private TextView mTv_intent_car_type;
    //意向配置
    private LinearLayout mLl_intent_car_configure;
    //意向配置显示
    private TextView mTv_intent_car_configure;
    //意向颜色
    private LinearLayout mLl_intent_color;
    //意向颜色显示
    private TextView mTv_intent_color;
    //竞品车型
    private LinearLayout mLl_compete_car_type;
    //竞品车型显示
    private TextView mTv_compete_car_type;
    //关注原因
    private LinearLayout mLl_notice_reason;
    //关注原因显示
    private TextView mTv_notice_reason;
//    //保存
//    private Button btn_save;
    //意图
    private Intent mIntent;
    //请求码
    private int requestCode;
    String user1=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_car_infor);
        //初始化组件
        mIv_Back = (ImageView) findViewById(R.id.iv_Back);
        mScrollView = (ScrollView) findViewById(R.id.scrollView);
        mLl_buy_use = (LinearLayout) findViewById(R.id.ll_buy_use);
        mTv_use = (TextView) findViewById(R.id.tv_use);
        mLl_buy_budget = (LinearLayout) findViewById(R.id.ll_buy_budget);
        mTv_budget = (TextView) findViewById(R.id.tv_budget);
        mLl_buy_way = (LinearLayout) findViewById(R.id.ll_buy_way);
        mTv_buy_way = (TextView) findViewById(R.id.tv_buy_way);
        mLl_car_user = (LinearLayout) findViewById(R.id.ll_car_user);
        mTv_car_user = (TextView) findViewById(R.id.tv_car_user);
        mLl_intent_car_series = (LinearLayout) findViewById(R.id.ll_intent_car_series);
        mTv_intent_car_series = (TextView) findViewById(R.id.tv_intent_car_series);
        mLl_intent_car_type = (LinearLayout) findViewById(R.id.ll_intent_car_type);
        mTv_intent_car_type = (TextView) findViewById(R.id.tv_intent_car_type);
        mLl_intent_car_configure = (LinearLayout) findViewById(R.id.ll_intent_car_configure);
        mTv_intent_car_configure = (TextView) findViewById(R.id.tv_intent_car_configure);
        mLl_intent_color = (LinearLayout) findViewById(R.id.ll_intent_color);
        mTv_intent_color = (TextView) findViewById(R.id.tv_intent_color);
        mLl_compete_car_type = (LinearLayout) findViewById(R.id.ll_compete_car_type);
        mTv_compete_car_type = (TextView) findViewById(R.id.tv_compete_car_type);
        mLl_notice_reason = (LinearLayout) findViewById(R.id.ll_notice_reason);
        mTv_notice_reason = (TextView) findViewById(R.id.tv_notice_reason);
//        btn_save= (Button) findViewById(R.id.btn_save);
        //设置点击事件
        mIv_Back.setOnClickListener(this);
        mLl_buy_use.setOnClickListener(this);
        mLl_buy_budget.setOnClickListener(this);
        mLl_buy_way.setOnClickListener(this);
        mLl_car_user.setOnClickListener(this);
        mLl_intent_car_series.setOnClickListener(this);
        mLl_intent_car_type.setOnClickListener(this);
        mLl_intent_car_configure.setOnClickListener(this);
        mLl_intent_color.setOnClickListener(this);
        mLl_compete_car_type.setOnClickListener(this);
        mLl_notice_reason.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //购买用途返回数据
        ArrayList use= data.getStringArrayListExtra("use");
        //购买预算返回数据
        String start = data.getStringExtra("start");
        String end = data.getStringExtra("end");
        //购买方式返回数据
        String way = data.getStringExtra("way");
        //车辆使用人返回数据
        ArrayList user = data.getStringArrayListExtra("user");
        //意向车系返回数据
        String series = data.getStringExtra("series");
        //意向车型返回数据
        String type = data.getStringExtra("type");
        //意向配置返回数据
        String configure = data.getStringExtra("configure");
        //意向颜色返回数据
        String color = data.getStringExtra("color");
        //竞品车型返回数据
        String compete = data.getStringExtra("compete");
        //关注原因返回数据
        ArrayList reason = data.getStringArrayListExtra("reason");
        // 根据上面发送过去的请求码来区别
        switch (requestCode) {
            case 0:
                if(type.equals("")){
                    mTv_intent_car_type.setText("未填");
                }else {
                    mTv_intent_car_type.setText(type);
                }
                break;
            case 1:
                if(configure.equals("")){
                    mTv_intent_car_configure.setText("未填");
                }else {
                    mTv_intent_car_configure.setText(configure);
            }
                break;
            case 2:
                if(color.equals(" ")){
                    mTv_intent_color.setText("未填");
                }else {
                    mTv_intent_color.setText(color);
                }
                break;
            case 3:
                if(CommonUtil.ergodicArray(user).equals(" ")){
                    mTv_car_user.setText("未填");
                }else {
                    mTv_car_user.setText(CommonUtil.ergodicArray(user));
                }
                break;
            case 4:
                if(start.equals("")&end.equals("")){
                    mTv_budget.setText("未填");
                }else {
                    mTv_budget.setText(start + "~" + end + "万元");
                }
                break;
            case 5:
                if(series.equals("")){
                    mTv_intent_car_series.setText("未填");
                }else {
                    mTv_intent_car_series.setText(series);
                }
                break;
            case 6:
                if(CommonUtil.ergodicArray(use).equals(" ")){
                    mTv_use.setText("未填");
                }else {
                    mTv_use.setText(CommonUtil.ergodicArray(use));
                }
                break;
            case 7:
                if(way.equals(" ")){
                    mTv_buy_way.setText("未填");
                }else {
                    mTv_buy_way.setText(way);
                }
                break;
            case 8:
                if(CommonUtil.ergodicArray(reason).equals(" ")){
                    mTv_notice_reason.setText("未填");
                }else {
                    mTv_notice_reason.setText(CommonUtil.ergodicArray(reason));
                }
                break;
            case 9:
                if(compete.equals(" ")){
                    mTv_compete_car_type.setText("未填");
                }else {
                    mTv_compete_car_type.setText(compete);
                }
                break;
            default:
                break;
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_Back:
                if(mTv_use.getText().toString().equals("")||mTv_budget.getText().toString().equals("")||mTv_buy_way.getText().toString().equals("")||mTv_intent_car_series.getText().toString().equals("")||mTv_intent_car_type.getText().toString().equals("")||mTv_intent_car_configure.getText().toString().equals("")||mTv_compete_car_type.getText().toString().equals("")||mTv_notice_reason.getText().toString().equals("")){
                    UIUtils.showToastSafe("必填项还没有选");
                }else {
                    //传值，返回数据
                    Intent mIntent = new Intent();
                    mIntent.putExtra("CarPurpose", mTv_use.getText().toString());
                    mIntent.putExtra("CarBudget", mTv_budget.getText().toString());
                    mIntent.putExtra("CarMode", mTv_buy_way.getText().toString());
                    mIntent.putExtra("CarUser", mTv_car_user.getText().toString());
                    mIntent.putExtra("CarSeries", mTv_intent_car_series.getText().toString());
                    mIntent.putExtra("CarModel", mTv_intent_car_type.getText().toString());
                    mIntent.putExtra("CarConfiguration", mTv_intent_car_configure.getText().toString());
                    mIntent.putExtra("CarColor", mTv_intent_color.getText().toString());
                    mIntent.putExtra("CompetingModels ", mTv_compete_car_type.getText().toString());
                    mIntent.putExtra("Reason", mTv_notice_reason.getText().toString());
                    // 设置结果，并进行传送
                    setResult(requestCode, mIntent);
                    finish();
                }
                break;
            case R.id.ll_intent_car_type:
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), TurnoverTypeActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 0;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_intent_car_configure:
                //跳转到成交配置界面
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), CarConfigureActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 1;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_intent_color:
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), CarColorActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 2;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_car_user:
//                showCarUserDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), CarUserActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 3;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_buy_budget:
//                showBuyBudgetDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), BuyBudgetActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 4;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_intent_car_series:
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), CarSeriesActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 5;
                startActivityForResult(mIntent, requestCode);
                break;

            case R.id.ll_buy_use:
//                showBuyUseDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), BuyCarUseActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 6;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_buy_way:
//                showBuyWayDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), BuyCarWayActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 7;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_notice_reason:
//                showFocusReasonDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), FocusReasonActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 8;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_compete_car_type:
//                showFocusReasonDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), CompeteCarTypeActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 9;
                startActivityForResult(mIntent, requestCode);
                break;
//            case R.id.btn_save:
//
//                break;
        }
    }
    /**
     * 自定义多选框
     */
    //购买用途
    private void showBuyUseDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_family_condition, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        //Listview
        final String[] beans = new String[] { "日常家用", "上下班代步", "自驾旅游", "接待公车", "公务车辆", "运营车辆"};
        ListView lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
        ListViewAdapter adapter=new ListViewAdapter(BuyCarInforActivity.this, beans);
        lv_checkBox.setAdapter(adapter);
        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.showToastSafe("保存成功");
                //得到选中的数据并发送
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //购买预算
    private void showBuyBudgetDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_price_region, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        //获取用户输入
        final EditText et_start= (EditText) view.findViewById(R.id.et_start);
        final EditText et_end= (EditText) view.findViewById(R.id.et_end);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取用户输入
                String start=et_start.getText().toString();
                String end=et_end.getText().toString();
                UIUtils.showToastSafe("用户选择了"+start+"万到"+end+"万的范围");
                //得到选中的数据并发送
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //购买方式
    private void showBuyWayDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_family_condition, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);

        //Listview
        final String[] beans = new String[] {"全款", "贷款", "融资租赁"};
        ListView lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
        ClientTradeAdapter adapter=new ClientTradeAdapter(BuyCarInforActivity.this, beans);
        lv_checkBox.setAdapter(adapter);
        lv_checkBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                UIUtils.showToastSafe("选中了"+i+"的"+beans[i]);
            }
        });
        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.showToastSafe("保存成功");
                //得到选中的数据并发送

                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //车辆使用人
    private void showCarUserDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_family_condition, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        //Listview
        final String[] beans = new String[] { "丈夫", "妻子", "子女", "父母", "领导","司机","公用"};
        ListView lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
        ListViewAdapter adapter=new ListViewAdapter(BuyCarInforActivity.this, beans);
        lv_checkBox.setAdapter(adapter);
        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.showToastSafe("保存成功");
                //得到选中的数据并发送
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //关注原因
    private void showFocusReasonDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_family_condition, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        //Listview
        final String[] beans = new String[] { "价格", "配置", "品牌", "服务", "网点位置","朋友推荐","安全性","舒适性","残值","使用成本"};
        ListView lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
        ListViewAdapter adapter=new ListViewAdapter(BuyCarInforActivity.this, beans);
        lv_checkBox.setAdapter(adapter);
        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.showToastSafe("保存成功");
                //得到选中的数据并发送
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //屏蔽系统返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键

        }
        return false;
    }
}
